import sys
import System
import System.Threading
import clr
import ee3
import parallel

clr.AddReferenceByPartialName("PresentationCore")
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("WindowsBase")
from System.Windows.Controls import *
from System import *

lb_nalog = Label()
lb_pass = Label()
lb_fail = Label()
lb_out = Label()
lb_todo = Label()
lb_stevec = Label()
tip = Label()
status = Label()
ee3.obrazec.stackPanel2.Children.Add(tip)
ee3.obrazec.stackPanel2.Children.Add(status)

def porocilo_nalog(sender, args):
    print("")

def init(): # inicializacija stevcev
    global lb_nalog
    global lb_pass
    global lb_fail
    global lb_out
    global lb_todo
    global lb_stevec
    global tip
    global status

    lb_stevec.Content = "0"
    ee3.obrazec.stackPanel5.Children.Add(lb_stevec)

    lb_1 = Label()
    lb_1.Content = "NALOG"
    lb_1.Margin = System.Windows.Thickness(140, 240, 25, 5)
    lb_1.MouseUp += porocilo_nalog
    ee3.obrazec.uiGridMain.Children.Add(lb_1)
    lb_2 = Label()
    lb_2.Content = "PASS"
    lb_2.Margin = System.Windows.Thickness(140, 252, 25, 5)
    lb_2.MouseUp += porocilo_nalog
    ee3.obrazec.uiGridMain.Children.Add(lb_2)
    lb_3 = Label()
    lb_3.Content = "FAIL"
    lb_3.Margin = System.Windows.Thickness(140, 264, 25, 5)
    lb_3.MouseUp += porocilo_nalog
    ee3.obrazec.uiGridMain.Children.Add(lb_3)
    lb_4 = Label()
    lb_4.Content = "KARAN"
    lb_4.Margin = System.Windows.Thickness(140, 276, 25, 5)
    lb_4.MouseUp += porocilo_nalog
    ee3.obrazec.uiGridMain.Children.Add(lb_4)
    lb_5 = Label()
    lb_5.Content = "TODO"
    lb_5.Margin = System.Windows.Thickness(140, 288, 25, 5)
    lb_5.MouseUp += porocilo_nalog
    ee3.obrazec.uiGridMain.Children.Add(lb_5)



    lb_nalog.FontWeight = System.Windows.FontWeights.Bold
    lb_nalog.Foreground = System.Windows.Media.Brushes.Black
    lb_nalog.Margin = System.Windows.Thickness(180, 240, 25, 5)
    ee3.obrazec.uiGridMain.Children.Add(lb_nalog)
    lb_pass.FontWeight = System.Windows.FontWeights.Bold
    lb_pass.Foreground = System.Windows.Media.Brushes.Green
    lb_pass.Margin = System.Windows.Thickness(180, 252, 25, 5)
    ee3.obrazec.uiGridMain.Children.Add(lb_pass)
    lb_fail.FontWeight = System.Windows.FontWeights.Bold
    lb_fail.Foreground = System.Windows.Media.Brushes.Orange
    lb_fail.Margin = System.Windows.Thickness(180, 264, 25, 5)
    ee3.obrazec.uiGridMain.Children.Add(lb_fail)
    lb_out.FontWeight = System.Windows.FontWeights.Bold
    lb_out.Foreground = System.Windows.Media.Brushes.Red
    lb_out.Margin = System.Windows.Thickness(180, 276, 25, 5)
    ee3.obrazec.uiGridMain.Children.Add(lb_out)
    lb_todo.FontWeight = System.Windows.FontWeights.Bold
    lb_todo.Foreground = System.Windows.Media.Brushes.Blue
    lb_todo.Margin = System.Windows.Thickness(180, 288, 25, 5)
    ee3.obrazec.uiGridMain.Children.Add(lb_todo)
    lb_nalog.Content = "n/a"
    lb_pass.Content = "n/a"
    lb_fail.Content = "n/a"
    lb_out.Content = "n/a"
    lb_todo.Content = "n/a"




def info(message = '', show=0):
    if (show):
        ee3.obrazec.image_info.Visibility = System.Windows.Visibility.Visible
        ee3.obrazec.image_info.ToolTip = '%s' %(message);
    else:
        ee3.obrazec.image_info.Visibility = System.Windows.Visibility.Hidden
        ee3.obrazec.image_info.ToolTip = ''

def stop(message = '', show=0):
    if (show):
        ee3.obrazec.image_stop.Visibility = System.Windows.Visibility.Visible
        ee3.obrazec.image_stop.ToolTip = '%s' %(message);
    else:
        ee3.obrazec.image_stop.Visibility = System.Windows.Visibility.Hidden
        ee3.obrazec.image_stop.ToolTip = '';

def ok(message = '', show=0):
    if (show):
        ee3.obrazec.image_ok.Visibility = System.Windows.Visibility.Visible
        ee3.obrazec.image_ok.ToolTip = '%s' %(message);
    else:
        ee3.obrazec.image_ok.Visibility = System.Windows.Visibility.Hidden
        ee3.obrazec.image_ok.ToolTip = '';

def warning(message = '', show=0):
    if (show):
        ee3.obrazec.image_warning.Visibility = System.Windows.Visibility.Visible
        ee3.obrazec.image_warning.ToolTip = '%s' %(message);
    else:
        ee3.obrazec.image_warning.Visibility = System.Windows.Visibility.Hidden
        ee3.obrazec.image_warning.ToolTip = ''

def brisiznake():
    info()
    stop()
    warning()
    ok()

def narisiznake(err='', warn='', inf='', fine=''):
    if (err != ''):
        stop(err, 1)
    else:
        stop()
    if (warn != ''):
        warning(warn, 1)
    else:
        warning()
    if (inf != ''):
        info(inf, 1)
    else:
        info()
    if (fine != ''):
        ok(fine, 1)
    else:
        ok()

def barva_obrazca(barva):    #{Red, Blue, Green};
    ee3.obrazec.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, Action[object](ee3.obrazec.ChangeGrid), barva)

def spremeni_plosco(sender, args):
    ee3.plosca = sender.Tag
    ee3.plosca.prikazi_na_obrazcu()

def paralelno_environment(pozicije_env):
    status_parallel = System.Windows.Controls.Primitives.StatusBar()
    for pozicije in pozicije_env.ToString().Split(';'):
        button = Button()
        button.Content = pozicije
        button.Click += spremeni_plosco
        status_parallel.Items.Add(button)
        neki = parallel.parallel(pozicije)
        neki.status = "INIT"
        button.Tag = neki
    if (ee3.obrazec.stackPanel6.Children.Count == 4):
       ee3.obrazec.stackPanel6.Children.RemoveAt(3)
    ee3.obrazec.stackPanel6.Children.Add(status_parallel)
    update_plosce(pozicije_env.ToString().Split(';')[0], "", None, None)

def update_plosce(pozicija, serijska, status=None, komentar=None):
    if (ee3.obrazec.stackPanel6.Children.Count == 4):
        for knofi in ee3.obrazec.stackPanel6.Children[3].Items:
            if (knofi.Tag.port == pozicija):
                  ee3.plosca = knofi.Tag
                  if (status != None):
                      ee3.plosca.plosca_status = status
                      if (status == "TESTING"):
                          ee3.ObdelajPlosco(None, serijska)
                      if (status == "IDLE"):
                          knofi.Tag = parallel.parallel(ee3.plosca.port)
                          ee3.plosca = knofi.Tag
                  if (komentar != None):
                      ee3.plosca.komentar = komentar
                  knofi.Tag.prikazi_na_obrazcu()